package cc.chinagps.gateway.unit.eg.pkg;

import java.util.Arrays;


import cc.chinagps.gateway.unit.eg.define.EGDefines;
import cc.chinagps.gateway.unit.eg.upload.bean.EGRealTimeInfo;
import cc.chinagps.gateway.util.HexUtil;

public class EGPackage {
	private EGHead head;

	private byte[] data;

	private byte xor;

	private byte[] source;

	public EGHead getHead() {
		return head;
	}

	public void setHead(EGHead head) {
		this.head = head;
	}

	public byte[] getData() {
		return data;
	}

	public void setData(byte[] data) {
		this.data = data;
	}

	public byte getXor() {
		return xor;
	}

	public void setXor(byte xor) {
		this.xor = xor;
	}

	public byte[] getSource() {
		return source;
	}

	public void setSource(byte[] source) {
		this.source = source;
	}

	public static EGPackage parse(byte[] source) throws Exception {
		EGPackage egPackage = new EGPackage();
		egPackage.setSource(source);
		EGHead head = new EGHead();
		int pos = 0;
		head = EGHead.parse(source, 0);
		egPackage.setHead(head);
		pos += EGDefines.HEAD_SIZE;
		int dataLen = head.getBodyLen();
		if (dataLen != source.length - EGDefines.HEAD_SIZE - 1) {
			throw new Exception("[EG] pkg len error");
		}
		byte[] data = new byte[dataLen];
		System.arraycopy(source, pos, data, 0, dataLen);
		egPackage.setData(data);
		pos += dataLen;

		byte xor = source[pos++];

		byte[] toXorByte = new byte[source.length - 1];
		System.arraycopy(source, 0, toXorByte, 0, toXorByte.length);
		byte xorCal = calXOR(toXorByte);

		if (xor != xorCal) {
			throw new Exception("[EG] pkg xor error,original xor :" + xor + ";computed xor :" + xorCal);
		}
		egPackage.setXor(xor);
		return egPackage;
	}

	private static byte calXOR(byte[] body) {
		byte xor = body[0];
		for (int i = 1; i < body.length; i++) {
			xor ^= body[i];
		}
		return xor;
	}

	public byte[] encode() throws Exception {
		if (data == null) {
			data = new byte[0];
		}
		byte[] source = new byte[data.length + EGDefines.HEAD_SIZE + 1];
		int pos = 0;
		byte[] headBytes = head.encode(data.length);
		System.arraycopy(headBytes, 0, source, pos, headBytes.length);
		pos += EGDefines.HEAD_SIZE;
		if (data != null) {
			System.arraycopy(data, 0, source, pos, data.length);
			pos += data.length;
		}

		byte[] toXorByte = new byte[source.length - 1];
		System.arraycopy(source, 0, toXorByte, 0, toXorByte.length);

		byte xorCal = calXOR(toXorByte);
		source[pos++] = xorCal;
		return source;
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("EGPackage [head=");
		builder.append(head);
		builder.append(", data=");
		builder.append(Arrays.toString(data));
		builder.append(", xor=");
		builder.append(xor);
		builder.append(", source=");
		builder.append(Arrays.toString(source));
		builder.append("]");
		return builder.toString();
	}

	public static void main(String[] args) throws Exception {
		String s = "23 23 02 4c 4a 31 38 52 32 42 4a 38 48 33 33 30 35 31 30 30 11 01 00 a1 13 0a 11 02 2d 0b 69 32 01 02 00 00 00 00 00 4c 4a 31 38 52 32 42 4a 38 48 33 33 30 35 31 30 30 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 00 00 00 00 00 00 00 00 00 0f a0 0f a0 00 00 00 00 00 00 00 00 00 00 00 07 06 cf bc e8 01 5a 1e ed 00 00 00 00 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 77 ";
		s = "23 23 01 4a 4c 43 31 32 39 37 37 38 39 30 30 30 31 36 33 34 01 01 00 1c 13 09 1d 0f 1a 15 00 01 38 39 38 36 30 36 31 37 30 34 30 30 35 33 32 32 30 37 37 30 58";
		s = "23 23 02 4a 4c 43 31 32 39 37 37 38 39 30 30 30 31 36 33 34 01 01 00 a1 13 09 1d 0f 1a 1b 00 01 01 02 fe 00 00 00 00 4a 4c 43 31 32 39 37 37 38 39 30 30 30 31 36 33 34 32 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 33 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 40 00 ff 89 8c 17 a0 00 8c 1b d0 1e 50 c8 01 a4 ff ff 30 80 04 e2 79 8c 01 00 00 00 00 00 00 00 00 00 0c dd e8 80 ff 23 00 00 00 00 62 08 ff 05 01 ff ff ff ff ff ff 6e";
		s = "23 23 03 4a 4c 43 31 32 39 37 37 38 39 30 30 30 31 36 33 34 01 01 00 a1 13 09 1d 0f 38 0f 00 01 01 02 fe 00 00 00 00 4a 4c 43 31 32 39 37 37 38 39 30 30 30 31 36 33 34 32 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 33 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 24 00 ff 89 80 17 a0 02 94 7f f8 1f f4 23 01 a4 ff ff 22 60 00 00 79 8c 01 00 00 00 00 00 00 00 00 00 00 0e 90 80 ff 23 00 00 00 00 80 00 00 00 c8 ff ff ff ff ff ff 43";
		s = "23 23 04 4a 4c 43 31 32 39 37 37 38 39 30 30 30 31 36 33 34 01 01 00 08 13 09 1d 0f 1b 1c 00 01 4c";
		s = "23 23 02 4c 4a 31 38 52 32 42 4a 38 48 33 33 30 35 31 30 30 11 01 00 a1 13 0a 11 0a 31 2d 02 1c 01 02 00 00 00 00 00 4c 4a 31 38 52 32 42 4a 38 48 33 33 30 35 31 30 30 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 00 00 00 00 00 00 00 00 00 0f a0 0f a0 00 00 00 00 00 00 00 00 00 00 00 00 06 cf bc e6 01 5a 1e df 00 00 00 00 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 3b ";
		s = "23 23 02 4c 4a 31 38 52 34 43 4c 30 48 33 33 31 35 35 35 34 11 01 00 a1 13 0a 11 0c 11 37 11 76 01 01 00 00 01 00 01 4c 4a 31 38 52 34 43 4c 30 48 33 33 31 35 35 35 34 54 45 53 54 5f 56 45 52 53 49 4f 4e 5f 56 31 2e 30 30 43 56 4e 5f 56 41 4c 55 45 00 00 00 00 00 00 00 00 00 49 55 50 52 5f 56 41 4c 55 45 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 00 3c 65 32 32 0d ac 00 05 05 dc 05 dc 3c 00 1e 03 e8 03 84 00 0a 46 32 00 06 cf bc f2 01 5a 1e de 00 00 04 4c 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ad ";
		s = "23 23 02 4a 4c 43 31 32 39 37 37 38 39 30 30 30 31 36 33 34 01 01 00 a1 13 09 1d 11 38 0a 05 a1 01 02 fe 00 00 00 00 4a 4c 43 31 32 39 37 37 38 39 30 30 30 31 36 33 34 32 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 33 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 24 00 ff 89 80 17 a0 02 94 7f f8 1f f4 23 ff 00 ff ff 22 60 00 00 79 8c 01 00 00 00 00 00 00 00 00 00 00 0e 90 80 ff 23 00 00 00 84 80 00 02 03 04 ff ff ff ff ff ff ef ";
		s = "2323024c4546434c434342394b5430353334373701010196130a1f0a071e02017dffff000000190000000000000000000000000000000000000006cfbcf7015a1ecc0000000002017dffff000000190000000000000000000000000000000000000006cfbcf7015a1ecc0000000002017dffff000000190000000000000000000000000000000000000006cfbcf7015a1ecc0000000002017dffff000000190000000000000000000000000000000000000006cfbcf7015a1ecc0000000002017dffff000000190000000000000000000000000000000000000006cfbcf7015a1ecc0000000002017dffff000000190000000000000000000000000000000000000006cfbcf7015a1ecd0000000002017dffff000000190000000000000000000000000000000000000006cfbcf7015a1ecd0000000002017dffff000000190000000000000000000000000000000000000006cfbcf7015a1ecd0000000002017dffff000000190000000000000000000000000000000000000006cfbcf7015a1ecd0000000002017dffff000000190000000000000000000000000000000000000006cfbcf7015a1ecd00000000a7";
		s ="2323024c37544144503433314b41303038363032160100a1130b0d0831062f9f010200000000004c37544144503433314b4130303836303200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000cd7d87000000000fa00fa09000001f40000000006400010720ee5901c7bfb200003480800c00000000003900000000000000000000e8";
		s = "2323814c4a31314c47475834433335374b4c36321601002e130b120e391200047473705f70617261244964547970653a303036362456616c547970653a30312456616c75653acc";
		s = "2323023030303030303030303030303030303030160100a1130c060b0d3a00b401020000000000303030303030303030303030303030303000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022700cd8884144000150fa0180ef000003d803d00000073000007222fbc01cd0f711d80000080000000000175500000000000000000000004";
		s = "23 23 02 4c 32 33 34 35 36 37 38 39 30 31 32 33 34 35 38 39 00 01 00 a1 14 01 07 0f 03 24 00 00 01 02 00 00 00 00 00 4c 32 33 34 35 36 37 38 39 30 31 32 33 34 35 38 39 54 45 53 54 5f 56 45 52 53 49 4f 4e 5f 56 31 2e 30 30 43 56 4e 5f 56 41 4c 55 45 30 30 30 30 30 30 30 30 30 49 55 50 52 5f 56 41 4c 55 45 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 32 00 00 00 06 cf bc f4 01 5a 1e ec 00 00 03 20 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 50";
		s = "2323024c5a474352325236584a58313135333536170100a11401070f0713790e010200000000004c5a474352325236584a5831313533353600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000cd9a862be000c30fa00fa07e00003259222000007200070000000000000000000737878001000000d7863c0000000000000000222028";
		s = "23230142534A43533030303030383738373837380201001C1402110F142B000230303030303030303031383737353235363234366A";
		s = "23230242534a4353303030303038373837383738020100b81402130a3b1500130102000000000042534a435330303030303837383738373830303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303000024400cce19b0280099c65906d60c800f0622066200082763c01ffffffffffffffff00000000800203000004d23200003039000004d229a0002100220023900021002200230f9100000000000000005f";
		s = "23230242534A4353303030303038373837383738020100B91402130E322A008B0102000000000042534A435330303030303837383738373830303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303000024400CCE19B0280099C65906D60C800F0622066200082763C010012D6870023CACE00000000800203000004D23200003039000004D229A00021002200239000210022002301910100000000000000009F";
		s = "23230242534A4353303030303038373837383738020100B81402130F211600580102000000000042534A435330303030303837383738373830303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303000024400CCE19B0280099C65906D60C800F0622066200082763C01FFFFFFFFFFFFFFFF00000000800203000004D23200003039000004D229A0002100220023900021002200230191000000000000000006";
		s = "2323024c45463030303030303038385341494745010100b3150512080d08739601000000000000ffffffffffffffffffffffffffffffffff000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000021810002091010008025000007d7d48e02710ffffffff00000022202220000028ff0006cfbcf2015a1ed600001edc80093300002f8428000000000000000022209101271000000000000061";
		s = "232302303030303030303030303030303030303014010067170103120803010200000000003030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030007f";
		s = "23238130303030303030303030303030303030300101012417060809021b00027473705f70617261244964547970653a383036352456616c547970653a30312456616c75653a30313c5345472a4e3a30303030303030303030303030303030303030302a443a3230332e3230372e3130342e3135382c383038372a613a31302f302a543a3131392e32332e3131312e3137312c32343030302a433a33302f302a43475245473a312a4353513a32372a4750533a31372a4e573a302c302a36443a30303030303030303030303030303030302a413a636d696f742a736569643a303030303030303030303030303030302c31362a4a313933393a312c3031303131302a69636369643a38393836303436323034313943313338383932312a4d435549443a3338373633383531333033363339303235323332333633373ea9";
		s = HexUtil.clearBlank(s);
		System.out.println(s);
		byte b[] = HexUtil.hexToBytes(s);
		EGPackage egPackage = EGPackage.parse(b);
		System.out.println(egPackage);
		
		
		byte data[] = egPackage.getData();
		int pos = 0;
		
		
		pos = 0;
		EGRealTimeInfo realTimeInfo = EGRealTimeInfo.parse(data, pos);
		System.out.println(realTimeInfo);
		
	}

}